
from apiview.view import APIView

# encoding: utf-8

from apiview import utility
from apiview.err_code import ErrCode
from apiview.exceptions import CustomError
from django.contrib.auth import authenticate, login

from apps.wechat import models, serializer


class Login(APIView):
    name = "小程序登录"

    def get_context(self, request, *args, **kwargs):
        session = wechat.wxa.code_to_session(request.params.js_code)
        wxa_user, new = models.User.objects.get_or_create(openid=session['openid'])
        wxa_user.set_info(session)
        login_user = authenticate(request, openid=wxa_user.openid)
        if login_user is None:
            raise CustomError(ErrCode.ERR_COMMON_BAD_PARAM)
        login(request, login_user)
        return serializer.UserSerializer(wxa_user, request=request).data

class UserBaseView(APIView):

    def check_api_permissions(self, request, *args, **kwargs):
        super(UserBaseView, self).check_api_permissions(request, *args, **kwargs)
        if not isinstance(request.user, models.User):
            raise CustomError(ErrCode.ERR_WECHAT_LOGIN)

    def get_context(self, request, *args, **kwargs):
        raise NotImplementedError


class UserInfo(UserBaseView):
    name = "小程序用户信息"
print(111)
    def get_context(self, request, *args, **kwargs):
        if request.params.encrypted_data or request.params.iv:
            try:
                data = decrypt_message(request.user.session_key, request.params.iv, request.params.encrypted_data)
            except Exception:
                utility.reportExceptionByMail("decrypt_message")
                raise CustomError(ErrCode.ERR_SYS_ERROR)
            request.user.set_info(data)
        return serializer.UserSerializer(request.user, request=request).data



